package com.system.controller;


import com.system.common.Cross;
import com.system.common.MD5;
import com.system.service.UserService;

import com.system.service.dao.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import javax.xml.ws.ResponseWrapper;
import java.util.Date;
import java.util.Random;

/**
 * Created by lenovo on 2016/3/5.
 */

@Controller
@RequestMapping("/user")
public class UserController {

   @Resource
   UserService userService;

    @RequestMapping("/login.do")
    @ResponseBody
      public  boolean login(HttpServletRequest request,@RequestBody User userpg) {
         //判断是不是跨域
         if (Cross.validateRequest(request)) {

             //对登陆密码做比较  加密密码加随机盐的判断
             User user=userService.getUser(userpg.getU_id().trim()); // id 是账户

             String passwd=user.getU_passwd(); //2从数据库中取出加密过后的密码(密码加随机数
             String bb=MD5.encodeMD5(userpg.getU_passwd())+user.getU_passwdrd();
             if(!passwd.equals(bb))
                 return false;
             HttpSession session = request.getSession();
             session.setAttribute("sessId", userpg.getU_id());

             //具体的非相似代码

             return true;
         }
         else
             return false ;
     }

    @RequestMapping("/regin.do")
    @ResponseBody
    public String  regin(@RequestBody User userpg){
        Random rd=new Random();
        Integer i=rd.nextInt(10);
        String passwd=MD5.encodeMD5(userpg.getU_passwd());
        String yan=MD5.MD5RandomMask(i);
        userpg.setU_passwd(passwd+yan);
        userpg.setU_passwdrd(yan);
        int b= userService.Insert(userpg);
        if(b==0){
            return "注册失败";
        }

        return "注册成功";
    }
}
